
/**
 * 字符串转时间戳
 */
function stringToTimestamp(dateString) {
    return new Date(dateString).getTime();
}

/**
 * 字符串转文本
 */
function stringToText(str){
	// console.log("stringToText: " + JSON.stringify(str));
	const time = stringToTimestamp(str)
	return timestampToText(time)
}

/**
 * 时间戳转文本
 */
function timestampToText(timestamp) {
	const minute = 1000 * 60; // 把分，时，天，周，半个月，一个月用毫秒表示
	const hour = minute * 60;
	const day = hour * 24;
	const week = day * 7;
	const now = new Date().getTime(); // 获取当前时间毫秒
	const diffValue = now - timestamp; // 时间差
	let result = "";
	if (diffValue < 0) {
		return;
	}
	const minC = diffValue / minute; // 计算时间差的分，时，天，周，月
	const hourC = diffValue / hour;
	const dayC = diffValue / day;
	const weekC = diffValue / week;
	if (weekC >= 1 && weekC <= 4) {
		result = ` ${parseInt(weekC, 10)}周前`;
	} else if (dayC >= 1 && dayC <= 6) {
		result = ` ${parseInt(dayC, 10)}天前`;
	} else if (hourC >= 1 && hourC <= 23) {
		result = ` ${parseInt(hourC, 10)}小时前`;
	} else if (minC >= 1 && minC <= 59) {
		result = ` ${parseInt(minC, 10)}分钟前`;
	} else if (diffValue >= 0 && diffValue <= minute) {
		result = "刚刚";
	} else {
		const datetime = new Date();
		datetime.setTime(timestamp);
		const Nyear = datetime.getFullYear();
		const Nmonth =
			datetime.getMonth() + 1 < 10 ?
			`0${datetime.getMonth() + 1}` :
			datetime.getMonth() + 1;
		const Ndate =
			datetime.getDate() < 10 ?
			`0${datetime.getDate()}` :
			datetime.getDate();
		result = `${Nyear}-${Nmonth}-${Ndate}`;
	}
	return result;
}

//十二星座
function getConstellation(dateString) {
	var date = new Date(dateString);
	var month = date.getMonth() + 1;
	var date = date.getDate();
	// console.log("month: " + JSON.stringify(month));
	// console.log("day: " + JSON.stringify(day));
    var constellations = ["水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"];
	var value;
    if (month == 1 && date >=20 || month == 2 && date <=18) {value = "水瓶座";}
    if (month == 2 && date >=19 || month == 3 && date <=20) {value = "双鱼座";}
    if (month == 3 && date >=21 || month == 4 && date <=19) {value = "白羊座";}
    if (month == 4 && date >=20 || month == 5 && date <=20) {value = "金牛座";}
    if (month == 5 && date >=21 || month == 6 && date <=21) {value = "双子座";}
    if (month == 6 && date >=22 || month == 7 && date <=22) {value = "巨蟹座";}
    if (month == 7 && date >=23 || month == 8 && date <=22) {value = "狮子座";}
    if (month == 8 && date >=23 || month == 9 && date <=22) {value = "室女座";}
    if (month == 9 && date >=23 || month == 10 && date <=22) {value = "天秤座";}
    if (month == 10 && date >=23 || month == 11 && date <=21) {value = "天蝎座";}
    if (month == 11 && date >=22 || month == 12 && date <=21) {value = "人马座";}
    if (month == 12 && date >=22 || month == 1 && date <=19) {value = "摩羯座";}
	return value;
}

// 多少年前的今天
function getYearAgo(yearAgo){
	const date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	year = year - yearAgo;
	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;
	return `${year}-${month}-${day}`;
}

module.exports = {
	timestampToText,
	stringToText,
	getConstellation,
	getYearAgo,
}